package lampachat_client;

import java.awt.Color;

/**
 *
 * @author alex
 */
public class JProfile extends javax.swing.JFrame {

    /**
     * Creates new form JProfile
     */
    JHome home;
    Message message = new Message();

    public JProfile(JHome home) {
        this.home = home;
        initComponents();
        this.setLocationRelativeTo(null);
        this.setEnabled(true);
        this.setVisible(true);
        jLabel2.setText(home.getUserName());
        getHideStatus();
    }

    private void getHideStatus() {
        String s = message.sendServiceMessage("", "getHideMode");
        if (s.equals("hide_mode isEnabled")) {
            jLabelHideStatus.setText("ON");
            jLabelHideStatus.setForeground(Color.decode("0x00cc00"));
            jButtonSetHideMode.setText("Disable");
        } else if (s.equals("hide_mode isDisabled")) {
            jLabelHideStatus.setText("OFF");
            jLabelHideStatus.setForeground(Color.decode("0x999999"));
            jButtonSetHideMode.setText("Enable");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JButtonCancel = new javax.swing.JButton();
        jButtonNewPassword = new javax.swing.JButton();
        jLabelNewPassword = new javax.swing.JLabel();
        jLabelNewPassword2 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();
        jLabelYourName = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelSetNewPass = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabelHideMode = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator5 = new javax.swing.JSeparator();
        jButtonSetHideMode = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelHideStatus = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        JButtonCancel.setText("Cancel");
        JButtonCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                JButtonCancelMouseClicked(evt);
            }
        });

        jButtonNewPassword.setText("Set new password");
        jButtonNewPassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNewPasswordMouseClicked(evt);
            }
        });

        jLabelNewPassword.setText("New Password");

        jLabelNewPassword2.setText("Confirm New Password");

        jLabelYourName.setForeground(new java.awt.Color(102, 102, 102));
        jLabelYourName.setText("Login:");

        jLabelSetNewPass.setText("Change Password");

        jLabelHideMode.setText("Hide Mode");

        jButtonSetHideMode.setText("HIDE");
        jButtonSetHideMode.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSetHideModeMouseClicked(evt);
            }
        });

        jLabel1.setText("<html><div>if the Hide mode is ON, you will not be shown</div>\n<div>in the search for new friends, but you can be</div>\n<div>friends if they know your Login</div>");

        jLabelHideStatus.setFont(new java.awt.Font("Liberation Sans", 0, 15)); // NOI18N
        jLabelHideStatus.setText("HideStat");

        jLabel2.setFont(new java.awt.Font("Liberation Sans", 1, 15)); // NOI18N
        jLabel2.setText("jLabel2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonSetHideMode, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 1, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(35, 35, 35)
                                        .addComponent(jLabelHideMode)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabelHideStatus))))
                            .addComponent(jButtonNewPassword)
                            .addComponent(jLabelNewPassword)
                            .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelNewPassword2)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator5)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(JButtonCancel))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelYourName)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabelSetNewPass)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelYourName)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelHideMode)
                    .addComponent(jLabelHideStatus))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonSetHideMode)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)))
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelSetNewPass)
                .addGap(18, 18, 18)
                .addComponent(jLabelNewPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelNewPassword2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonNewPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(JButtonCancel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        //System.out.println("JP OPEN");
    }//GEN-LAST:event_formWindowOpened

    private void JButtonCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_JButtonCancelMouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_JButtonCancelMouseClicked

    private void jButtonSetHideModeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSetHideModeMouseClicked
        // TODO add your handling code here:
        if (jLabelHideStatus.getText().equals("ON")) {
            message.sendServiceMessage("off", "SetHideMode");
        } else {
            message.sendServiceMessage("on", "SetHideMode");
        }
        getHideStatus();
    }//GEN-LAST:event_jButtonSetHideModeMouseClicked

    private void jButtonNewPasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonNewPasswordMouseClicked
        // TODO add your handling code here:

        String pass = String.valueOf(jPasswordField1.getPassword());
        if (pass.length() > 3) {
            String pass2 = String.valueOf(jPasswordField2.getPassword());
            if (pass.equals(pass2)) {
                String[] stringToSearch = {";", "\"", " ", "+", "\\"};
                boolean trig = true;
                for (String s : stringToSearch) {
                    if (pass.contains(s)) {
                        System.out.println("fail pass- " + pass);
                        trig = false;
                        new JInfoWindow().setText("Password should not contain:\n" + " ;" + " \"" + " space " + " +");
                        break;
                    }
                }
                if (trig) {
                    if (message.sendServiceMessage(pass, "setPassword").equals("ok")) {
                        Database database = new Database();
                        database.setLoginAndPass(home.getUserName(), pass);
                        new JInfoWindow(home).setText("Your password has been changed successfully.\nPlease RESTART app!");
                    } else {
                        new JInfoWindow().setText("Error password updating");
                    }
                }
            } else {
                new JInfoWindow().setText("Your password and confirmation password do not match!");
            }
        } else {
            new JInfoWindow().setText("New password is too short!\n4 characters minimum!");

        }
    }//GEN-LAST:event_jButtonNewPasswordMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JProfile.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JProfile.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JProfile.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JProfile.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        // java.awt.EventQueue.invokeLater(new Runnable() {
        //   public void run() {
        //     new JProfile().setVisible(true);
        //}
        //});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JButtonCancel;
    private javax.swing.JButton jButtonNewPassword;
    private javax.swing.JButton jButtonSetHideMode;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelHideMode;
    private javax.swing.JLabel jLabelHideStatus;
    private javax.swing.JLabel jLabelNewPassword;
    private javax.swing.JLabel jLabelNewPassword2;
    private javax.swing.JLabel jLabelSetNewPass;
    private javax.swing.JLabel jLabelYourName;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    // End of variables declaration//GEN-END:variables
}
